package com.hsnn.medstgmini.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class ParseTimes {
/**
 * 求今天是星期几
 * @param pTime
 * @return
 * @throws Exception
 */
	public  static  int  dayForWeek(String pTime) throws  Exception {  
		SimpleDateFormat	format = new  SimpleDateFormat("yyyy-MM-dd" );  
		 Calendar c = Calendar.getInstance();  
		 c.setTime(format.parse(pTime));  
		 int  dayForWeek = 0 ;  
		 if (c.get(Calendar.DAY_OF_WEEK) == 1 ){  
		  dayForWeek = 7 ;  
		 }else {  
		  dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1 ;  
		 }  
		 return  dayForWeek;  
		} 
	
	public static void getTime(Date date) {
        if(date == null) date=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int dow=calendar.get(Calendar.DAY_OF_WEEK);
        if(dow==7) dow=0;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        //本周最小日期
        calendar.add(Calendar.DAY_OF_YEAR, -dow);
        String strDate=sdf.format(calendar.getTime());
         
//      本周最大日期
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        strDate=sdf.format(calendar.getTime());
    }
	
	
	/**
	 * 
	 * @param flag
	 *            星期（1-7）
	 * @return 日期（2012-11-08）
	 */
	public static String sDateByMonth(int w) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar c = new GregorianCalendar();
		// 如果大于7或者小于1,返回当前日期
		if (w > c.DAY_OF_WEEK || w < 1) {
			return sdf.format(c.getTime());
		}
		int curWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (curWeek < w) {
			c.add(Calendar.DATE, w - curWeek);
		} else if (curWeek > w) {
			c.add(Calendar.DATE, c.DAY_OF_WEEK - (curWeek - w));
		}
		return sdf.format(c.getTime());
	}
}